<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索用户和添加用户 -->
    <el-input placeholder="请输入内容" v-model="searchkey" style="width:300px" @keyup.native.enter="searckList">
      <el-button slot="append" icon="el-icon-search" @click="searckList"> </el-button>
    </el-input>
    <el-button type="success" plain @click="adddialogFormVisible=true">添加用户</el-button>
    <!-- 表格 -->
    <el-table :data="usersList" border style="width: 100%">
      <el-table-column type="index" width="50">
      </el-table-column>
      <el-table-column prop="username" label="姓名" width="180">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="180">
      </el-table-column>
      <el-table-column prop="mobile" label="电话">
      </el-table-column>
      <!-- 状态 -->
      <el-table-column prop="address" label="状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.mg_state" active-color="#13ce66" inactive-color="#ff4949" @change="changeStatus(scope.row)">
          </el-switch>

        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button type="success " icon="el-icon-edit" plain @click="editUsers(scope.row)"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button type="danger " icon="el-icon-edit" plain @click="delUsers(scope.row)"></el-button>
          </el-tooltip>
          <el-tooltip content="授权角色" placement="top">
            <el-button type="primary" icon="el-icon-edit" plain @click="grantUser(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pagenum" :page-sizes="[10, 20, 30, 40]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <!-- 添加用户对话框 -->
    <el-dialog title="添加用户" :visible.sync="adddialogFormVisible">
      <el-form :model="addform" label-width="100px" :rules="rules" ref="addform">
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="addform.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="password">
          <el-input v-model="addform.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addform.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="addform.mobile" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" prop="username">
        <el-button @click="adddialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserSubmit('addform')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑用户对话框 -->
    <el-dialog title="编辑用户" :visible.sync="editdialogFormVisible">
      <el-form :model="editform" label-width="100px" :rules="rules" ref="editform">
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="editform.username" auto-complete="off" disabled=""></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editform.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="editform.mobile" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" prop="username">
        <el-button @click="editdialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserSubmit('editform')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 授权角色对话框 -->
    <el-dialog title="授权角色" :visible.sync="grantdialogFormVisible">
      <el-form :model="grantform" label-width="100px" :rules="rules" ref="grantform">
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="grantform.username" auto-complete="off" disabled="" style="width:200px"></el-input>
        </el-form-item>
        <!-- select选择器 -->
        <el-form-item label="角色">
          <template>
            <el-select v-model="roleValue" placeholder="请选择" @change="changeRoles">
              <el-option v-for="item in rolesList" :key="item.value" :label="item.roleName" :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="grantdialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="grantUserSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getAllUsersData,
  addUsers,
  editUsers,
  deleteUsers,
  changeUsersStatusById,
  rolesListApi,
  rolesUserApi
} from "@/api/index.js";
export default {
  data() {
    return {
      rolesList: [],
      roleValue: "",
      // 验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: "blur,change"
          }
        ],
        mobile: [{ required: true, message: "电话不能为空" }]
      },
      //   授权管理对话框
      grantdialogFormVisible: false,
      //  编辑用户对话框
      editdialogFormVisible: false,
      // 添加用户对话框
      adddialogFormVisible: false,
      grantform: {
        username: "",
        id: "",
        rid: ""
      },
      addform: {
        username: "",
        password: "",
        email: "",
        mobile: ""
      },
      editform: {
        id: "",
        username: "",
        email: "",
        mobile: ""
      },
      // 搜索
      searchkey: "",
      value2: "",
      //   分页
      total: 0,
      pagenum: 1,
      pagesize: 10,
      usersList: []
    };
  },
  methods: {
    // 授权角色用户对话框提交
    grantUserSubmit() {
      rolesUserApi(this.grantform).then(res => {
        console.log(res);
        if (res.meta.status == 200) {
          this.$message({
            type: "success",
            message: res.meta.msg
          });
          this.init()
        } else {
          this.$message({
            type: "warning",
            message: res.meta.msg
          });
        }
        this.grantdialogFormVisible = false;
      });
    },
    // 选中下拉框角色
    changeRoles() {
      console.log(this.roleValue);
      this.grantform.rid = this.roleValue;
    },
    //   授权角色
    grantUser(row) {
      console.log(row);
      this.grantdialogFormVisible = true;
      this.grantform.id = row.id;
      this.grantform.username = row.username;
      rolesListApi().then(res => {
        console.log(res);
        this.rolesList = res.data;
      });
    },
    //   改变用户状态
    changeStatus(row) {
      console.log(row);
      changeUsersStatusById({ id: row.id, status: row.mg_state }).then(res => {
        console.log(res);
        if (res.meta.status == 200) {
          this.$message({
            type: "success",
            message: res.meta.msg
          });
        } else {
          this.$message({
            type: "warning",
            message: res.meta.msg
          });
        }
      });
    },
    // 删除单个用户
    delUsers(pa) {
      this.$confirm(`此操作将永久删除${pa.username}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteUsers(pa).then(res => {
            console.log(res);
            if (res.meta.status == 200) {
              this.$message({
                type: "success",
                message: res.meta.msg
              });
              this.init();
            } else {
              this.$message({
                type: "warning",
                message: res.meta.msg
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    //   编辑用户
    editUserSubmit(editform) {
      this.$refs[editform].validate(valid => {
        if (valid) {
          editUsers(this.editform).then(res => {
            console.log(res);
            if (res.meta.status == 200) {
              console.log(res.meta.status);
              this.$message({
                message: res.meta.msg,
                type: "success"
              });
              this.editdialogFormVisible = false;
              this.$refs[editform].resetFields();
              this.init();
            } else {
              console.log(res.meta.status);
              this.$message({
                message: res.meta.msg,
                type: "warning"
              });
            }
          });
        } else {
          this.$message.error("输入的参数有误");
        }
      });
    },
    //   添加用户
    addUserSubmit(addform) {
      this.$refs[addform].validate(valid => {
        if (valid) {
          addUsers(this.addform).then(res => {
            console.log(res);
            if (res.meta.status == 201) {
              console.log(res.meta.status);
              this.$message({
                message: res.meta.msg,
                type: "success"
              });
              this.adddialogFormVisible = false;
              this.$refs[addform].resetFields();
              this.init();
            } else {
              console.log(res.meta.status);
              this.$message({
                message: res.meta.msg,
                type: "warning"
              });
            }
          });
        } else {
          this.$message.error("输入的参数有误");
        }
      });
    },
    //   搜索功能
    searckList() {
      this.init();
      console.log(123);
    },
    //   分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pagesize = val;
      this.init();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagenum = val;
      this.init();
    },
    // 编辑按钮操作
    editUsers(scop) {
      console.log(scop);
      this.editform.username = scop.username;
      this.editform.email = scop.email;
      this.editform.mobile = scop.mobile;
      this.editform.id = scop.id;
      this.editdialogFormVisible = true;
    },
    init() {
      // 获取所有用户数据
      getAllUsersData({
        query: this.searchkey,
        pagenum: this.pagenum,
        pagesize: this.pagesize
      }).then(res => {
        console.log(res);
        // console.log(res.data.users);
        this.usersList = res.data.data.users;
        this.total = res.data.total;
      });
    }
  },
  mounted() {
    this.init();
  }
};
</script>

<style lang="scss" scoped>
.el-breadcrumb {
  background-color: #f5f5f5;
  height: 45px;
  font-size: 15px;
  padding-left: 10px;
  line-height: 45px;
  margin-bottom: 15px;
}
</style>